package com.mjfeed.controller.admin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mjfeed.controller.BaseController;
import com.mjfeed.entity.ForumTopic;
import com.mjfeed.entity.ProductCategory;
import com.mjfeed.service.ForumTopicService;
import com.mjfeed.service.ProductCategoryService;
import com.mjfeed.util.Format;
import com.mjfeed.util.Pagination;
import com.mjfeed.util.ParamBean;

/**
 * @description 论坛话题
 * @author zhaominglei
 * @date 2015年5月24日
 */
@Controller("adminForumTopicController")
@RequestMapping("/admin")
public class ForumTopicController extends BaseController {
    @Resource(name="forumTopicService")
    private ForumTopicService forumTopicService;
    @Resource(name="productCategoryService")
	private ProductCategoryService productCategoryService;
    
    @RequestMapping(value = "/forumTopic/list", method=RequestMethod.GET)
	public String list(ParamBean paramBean, HttpServletRequest request, HttpServletResponse response, Model model){
		paramBean.setUrl(getBasePath(request) + "admin/forumTopic/list.shtml");
		int p = (Format.null2Zero(request.getParameter("p")) == 0) ? 1 : Format.null2Zero(request.getParameter("p"));
		paramBean.setPage(p);
		Pagination<ForumTopic> pagination = forumTopicService.getPagination(paramBean);
		request.setAttribute("pagination", pagination);
		return "admin/forumTopic/list";
	}
	
	@RequestMapping(value = "/forumTopic/add",method=RequestMethod.GET)
	public String add(HttpServletRequest request, HttpServletResponse response, Model model){
		return "admin/forumTopic/add";
	}
	
	@RequestMapping(value = "/forumTopic/save", method=RequestMethod.POST)
	public String save(ForumTopic forumTopic, HttpServletRequest request, HttpServletResponse response, Model model){
		return "redirect:/admin/forumTopic/list.shtml";
	}
	
	@RequestMapping(value = "/forumTopic/edit",method=RequestMethod.GET)
	public String edit(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("forumTopic", forumTopicService.getById(ForumTopic.class, id));
		return "admin/forumTopic/edit";
	}
	
	@RequestMapping(value = "/forumTopic/update", method=RequestMethod.POST)
	public String update(ForumTopic forumTopic, HttpServletRequest request, HttpServletResponse response, Model model){
		return "redirect:/admin/forumTopic/list.shtml";
	}
	
	@RequestMapping(value = "/forumTopic/view",method=RequestMethod.GET)
	public String view(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("forumTopic", forumTopicService.getById(ForumTopic.class, id));
		return "admin/forumTopic/view";
	}
	
    @RequestMapping(value = "/collegeTopic/list", method=RequestMethod.GET)
	public String collegeTopicList(ParamBean paramBean, HttpServletRequest request, HttpServletResponse response, Model model){
		paramBean.setUrl(getBasePath(request) + "admin/collegeTopic/list.shtml");
		int p = (Format.null2Zero(request.getParameter("p")) == 0) ? 1 : Format.null2Zero(request.getParameter("p"));
		paramBean.setPage(p);
		paramBean.setForumType(ForumTopic.COLLEGE_TOPIC_TYPE);
		Pagination<ForumTopic> pagination = forumTopicService.getPagination(paramBean);
		request.setAttribute("pagination", pagination);
		return "admin/collegeTopic/list";
	}
	
	@RequestMapping(value = "/collegeTopic/add",method=RequestMethod.GET)
	public String collegeTopicAdd(HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("productCategoryTree", productCategoryService.findRoots());
		return "admin/collegeTopic/add";
	}
	
	@RequestMapping(value = "/collegeTopic/save", method=RequestMethod.POST)
	public String collegeTopicSave(ForumTopic forumTopic, HttpServletRequest request, HttpServletResponse response, Model model){
		String productCategoryId = request.getParameter("productCategoryId");
		if (productCategoryId == null || productCategoryId.equals("")) {
			return ERROR_VIEW;
		}
		ProductCategory productCategory = productCategoryService.getById(ProductCategory.class, Long.valueOf(productCategoryId));
		if (productCategory == null) {
			return ERROR_VIEW;
		}
		forumTopic.setProductCategory(productCategory);
		forumTopic.setType(ForumTopic.COLLEGE_TOPIC_TYPE);
		forumTopic.setIp(getRemoteIpAddr(request));
		forumTopicService.build(forumTopic);
		forumTopicService.save(forumTopic);
		return "redirect:/admin/collegeTopic/list.shtml";
	}
	
	@RequestMapping(value = "/collegeTopic/edit",method=RequestMethod.GET)
	public String collegeTopicEdit(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("collegeTopic", forumTopicService.getById(ForumTopic.class, id));
		request.setAttribute("productCategoryTree", productCategoryService.findRoots());
		return "admin/collegeTopic/edit";
	}
	
	@RequestMapping(value = "/collegeTopic/update", method=RequestMethod.POST)
	public String collegeTopicUpdate(ForumTopic forumTopic, HttpServletRequest request, HttpServletResponse response, Model model){
		ForumTopic pForumTopic = forumTopicService.getById(ForumTopic.class, forumTopic.getId());
		if (pForumTopic == null) {
			return ERROR_VIEW;
		}
		String productCategoryId = request.getParameter("productCategoryId");
		if (productCategoryId == null || productCategoryId.equals("")) {
			return ERROR_VIEW;
		}
		ProductCategory productCategory = productCategoryService.getById(ProductCategory.class, Long.valueOf(productCategoryId));
		if (productCategory == null) {
			return ERROR_VIEW;
		}
		forumTopic.setProductCategory(productCategory);
		forumTopic.setType(ForumTopic.COLLEGE_TOPIC_TYPE);
		forumTopic.setIp(getRemoteIpAddr(request));
		forumTopicService.build(forumTopic);
		forumTopic.setSource((forumTopic.getSource() == null || forumTopic.getSource().equals("") ? pForumTopic.getSource() : forumTopic.getSource()));
		forumTopicService.update(forumTopic);
		return "redirect:/admin/collegeTopic/list.shtml";
	}
	
	@RequestMapping(value = "/collegeTopic/view",method=RequestMethod.GET)
	public String collegeTopicView(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("collegeTopic", forumTopicService.getById(ForumTopic.class, id));
		request.setAttribute("productCategoryTree", productCategoryService.findRoots());
		return "admin/collegeTopic/view";
	}	
}